package com.swust.course.service.impl;

import com.alibaba.csp.sentinel.util.AssertUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swust.common.core.exception.ServiceException;
import com.swust.common.core.utils.DateUtils;
import com.swust.common.core.utils.MyAssertUtil;
import com.swust.course.domain.CourseInfo;
import com.swust.course.domain.CoursePlan;
import com.swust.course.mapper.CoursePlanMapper;
import com.swust.course.service.ICourseInfoService;
import com.swust.course.service.ICoursePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 开课计划Service业务层处理
 *
 * @author LiuShuai
 * @date 2024-04-05
 */
@Service
public class CoursePlanServiceImpl extends ServiceImpl<CoursePlanMapper, CoursePlan> implements ICoursePlanService {
    @Autowired
    private CoursePlanMapper coursePlanMapper;

    @Autowired
    private ICourseInfoService iCourseInfoService;

    /**
     * 查询开课计划
     *
     * @param courseSerialNo 开课计划主键
     * @return 开课计划
     */
    @Override
    public CoursePlan selectCoursePlanByCourseSerialNo(String courseSerialNo) {
        return coursePlanMapper.selectCoursePlanByCourseSerialNo(courseSerialNo);
    }

    /**
     * 查询开课计划列表
     *
     * @param coursePlan 开课计划
     * @return 开课计划
     */
    @Override
    public List<CoursePlan> selectCoursePlanList(CoursePlan coursePlan) {
        return coursePlanMapper.selectCoursePlanList(coursePlan);
    }

    /**
     * 新增开课计划
     *
     * @param coursePlan 开课计划
     * @return 结果
     */
    @Override
    public int insertCoursePlan(CoursePlan coursePlan) {
        coursePlan.setCreateTime(DateUtils.getNowDate());
        return coursePlanMapper.insertCoursePlan(coursePlan);
    }

    /**
     * 修改开课计划
     *
     * @param coursePlan 开课计划
     * @return 结果
     */
    @Override
    public int updateCoursePlan(CoursePlan coursePlan) {
        coursePlan.setUpdateTime(DateUtils.getNowDate());
        return coursePlanMapper.updateCoursePlan(coursePlan);
    }

    /**
     * 批量删除开课计划
     *
     * @param courseSerialNos 需要删除的开课计划主键
     * @return 结果
     */
    @Override
    public int deleteCoursePlanByCourseSerialNos(String[] courseSerialNos) {
        return coursePlanMapper.deleteCoursePlanByCourseSerialNos(courseSerialNos);
    }

    /**
     * 删除开课计划信息
     *
     * @param courseSerialNo 开课计划主键
     * @return 结果
     */
    @Override
    public int deleteCoursePlanByCourseSerialNo(String courseSerialNo) {
        return coursePlanMapper.deleteCoursePlanByCourseSerialNo(courseSerialNo);
    }

    @Override
    @Transactional
    public String importCourseInfo(List<CoursePlan> coursePlans, boolean updateSupport, String operName) {
        AssertUtil.assertNotEmpty(coursePlans, "导入数据不能为空");
        final List<CourseInfo> courseInfoAll = iCourseInfoService.list();
        MyAssertUtil.assertNotEmpty(courseInfoAll, "请先导入课程信息");
        final List<String> stringsCourseNo = courseInfoAll.stream()
                .map(CourseInfo::getCourseCode)
                .collect(Collectors.toList());
        for (CoursePlan coursePlan : coursePlans) {
            final String courseNo = coursePlan.getCourseNo();
            if (!stringsCourseNo.contains(courseNo)) {
                throw new ServiceException("课程代码" + courseNo + "不存在,请检查");
            }
        }
        //生成课序号:课程代码:开始周数:结束周数:开课校区
        for (CoursePlan coursePlan : coursePlans) {
            if(coursePlan.getCourseSerialNo()==null){
                coursePlan.setCourseSerialNo(coursePlan.getCourseNo() + ":"
                        + coursePlan.getStartWeek() + ":"
                        + coursePlan.getEndWeek() + ":" + coursePlan.getSchoolArea());
            }
        }
        this.saveOrUpdateBatch(coursePlans);
        return "导入成功";
    }
}
